//
//  IWNewFeatureViewController.m
//  8期微博
//
//  Created by apple on 14-9-4.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "TTNewFeatureViewController.h"
#import "TTTabBarController.h"

#define IWNewfeatureImageCount  4

@interface TTNewFeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *control;
@end

@implementation TTNewFeatureViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 1.初始化UISrollView
    [self setupSrollView];
    
    // 2.初始化UIPageControl
    [self setupPageControl];
}
/**
 * 初始化UIPageControl
 */
- (void)setupPageControl
{
    // 1.创建UIPageControl
    UIPageControl *control = [[UIPageControl alloc] init];
    control.numberOfPages = IWNewfeatureImageCount;
    
    // 2.设置UIPageControl的frame
    control.centerX = self.view.width * 0.5;
    control.centerY = self.view.height - 30;
    
    // 3.设置UIPageControl其它属性
    // 3.1设置当前页的颜色
    control.currentPageIndicatorTintColor = [UIColor redColor];
    // 3.2设置其它页的颜色
    control.pageIndicatorTintColor = [UIColor greenColor];
    
    // 4.添加UIPageControl到控制器view
    [self.view addSubview:control];
    self.control = control;
}

/*
  imageName: 加载图片
  自动根据当前屏幕的分辨率加载图片
  如果当前是非retain屏幕 --> 加载new_feature_1.png
  如果当前是retain/3.5inch屏幕   --> 加载new_feature_1@2x.png
  但是如果如果当前是retain/4inch屏幕 -->不会自动加载new_feature_1-568h@2x.png
  只有是启动图片的时候, 系统才会自动加载-568h@2x.png的图片 其他情况都不会自动加载
 */
/**
 * 初始化UISrollView
 */
- (void)setupSrollView
{
    // 1.添加UISrollView
    UIScrollView *scrollerView = [[UIScrollView alloc] init];
    scrollerView.frame = self.view.bounds;
    [self.view addSubview:scrollerView];
    scrollerView.delegate = self;
    
    // 2.添加图片
    CGFloat width = scrollerView.width;
    CGFloat height = scrollerView.height;
    
    for (int i = 0; i < IWNewfeatureImageCount; i++) {
        
        // 1.拼接图片名称
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i+1];
//        IWLog(@"%@", name);
        // 1.1判断当前屏幕的尺寸
        if (FourInch) {
            name = [name stringByAppendingString:@"-568h"];
        }
        IWLog(@"%@", name);
        UIImage *image = [UIImage imageWithName:name];
        // 2.创建UIImageView
        UIImageView *iv = [[UIImageView alloc] init];
        iv.image = image;
        
        // 3.设置frame
        iv.width = width;
        iv.height = height;
        iv.y = 0;
        iv.x = i * iv.width;
        
        // 4.添加UIImageView到scrollerView
        [scrollerView addSubview:iv];
        
        // 5.拿到最后一张图片添加按钮
        if (i == IWNewfeatureImageCount - 1) {
            
            // 0.设置父控件可以交互
            iv.userInteractionEnabled = YES;
            
            // 1.添加开始按钮
            [self setupStartBtn:iv];
            
            // 2.添加分享按钮
            [self setupShareBtn:iv];
            
        }
    }
    // 设置UISrollView的其他属性
    // 设置contentsize
    scrollerView.contentSize = CGSizeMake(IWNewfeatureImageCount * width, 0);
    // 设置分页
    scrollerView.pagingEnabled = YES;
    // 隐藏指示条
    scrollerView.showsHorizontalScrollIndicator = NO;
    // 设置没有弹簧效果
    scrollerView.bounces = NO;
}

/**
 * 添加分享按钮
 */
- (void)setupShareBtn:(UIImageView *)imageview;
{
    // 1.添加分享按钮
    UIButton *shareButton = [[UIButton alloc] init];
    [imageview addSubview:shareButton];
    // 2.设置文字和颜色
    [shareButton setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 3.设置图标
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
    // 4.监听按钮点击事件
    [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    // 5.设置frame
    shareButton.width = 200;
    shareButton.height = 35;
    shareButton.centerX = self.view.width * 0.5;
    shareButton.centerY = self.view.height * 0.7;
    
    shareButton.adjustsImageWhenHighlighted = NO;
    
    // 6.设置内边距(内切 自宫)
    //标题的内边距
    shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
}
- (void)shareButtonClick:(UIButton *)shareBtn
{
    shareBtn.selected = !shareBtn.selected;
}

/**
 * 添加开始按钮
 */
- (void)setupStartBtn:(UIImageView *)imageview;
{
    // 1.创建按钮
    UIButton *startButton = [[UIButton alloc] init];
    [imageview addSubview:startButton];
    
    // 2.设置背景图片
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    
    // 3.设置文字
    [startButton setTitle:@"开始微博" forState:UIControlStateNormal];
    
    // 4.设置frame
    startButton.size = startButton.currentBackgroundImage.size;
    startButton.centerX = self.view.width * 0.5;
    startButton.centerY = self.view.height * 0.8;
    
    // 5.监听按钮点击事件
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
}

/**
 * 开始微博点击事件
 */
- (void)start
{
    // 0.显示状态栏
    UIApplication *app = [UIApplication sharedApplication];
    app.statusBarHidden = NO;
    
    // 1.创建主界面控制器
    TTTabBarController *tabBarVc = [[TTTabBarController alloc] init];
    
    // 2.切换控制器
    UIWindow *window = app.keyWindow;
    window.rootViewController = tabBarVc;
}

#pragma mark - UIScrollViewDelegate
// 1.性能不好
// 2.稍微动一下就会变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    // 1.计算页码
    double ratio = scrollView.contentOffset.x / self.view.width;
    int page = (int)(ratio + 0.5);
    
    // 2.设置页码
    self.control.currentPage = page;
}

@end
